<?php
/**
 * OutsourceOrder.php
 * Author: luoji (867404364@qq.com)
 * Date: 2020-12-21
 * Time: 14:32
 */

namespace App\Http\Controllers\Api\Business;


use App\Exceptions\RunException;
use App\Http\Controllers\Base;
use App\Tables;


class OutsourceOrder extends Base
{
    public function getList()
    {
        $list = Tables\OrderDetail::where('outsource_id' , '>', 0)
            ->select('order_detail.*')
            ->leftJoin('order', 'order.id', '=', 'order_detail.oid')
            ->where('order.dumped', Tables\Order::No_Dumped)
            ->where('order_detail.make_status', '!=', Tables\Order::Make_Status_Ok)
            ->orderBy('order_detail.id', 'desc')
            ->get();
        $r = [];
        foreach($list as $v) {
            $cc = $pp = [];
            foreach($v->consumables()->where('bid', $v['bid'])->get() as $c) {
                $cc[] = $c['c_name'];
            }
            foreach($v->postPrints()->where('bid', $v['bid'])->get() as $p) {
                $pp[] = $p['p_name'];
            }
            $r[] = [
                'id' => $v['id'],
                'customerName' => $v->order()->value('customer_name') ?? '',
                'businessName' => $v['b_name'],
                'outsourceName' => $v['outsource_name'],
                'content' => $v['content'],
                'consumable' => $cc,
                'postPrint' => $pp,
                'completeDate' => date('Y-m-d', strtotime($v['complete_date'])),
                'makeStatus' => $v['make_status']
            ];
        }
        return $this->responseSuccess($r);
    }

    public function setMake()
    {
        $id = (array) $this->request->post('id', []);
        $details = Tables\OrderDetail::whereIn('id', $id)->get();
        if(empty($details)) {
            return $this->responseFail('未找到信息');
        }

        \DB::beginTransaction();
        try {
            foreach($details as $d) {
                $d['make_status'] = Tables\Order::Make_Status_Ing;
                $o = $d->order;
                $o['make_status'] = Tables\Order::Make_Status_Ing;
                $o['update_time'] = date('Y-m-d H:i:s');
                if(!(bool) $o->save()) { throw new RunException('操作失败(1)');}
                if(!(bool) $d->save()) {throw new RunException('操作失败(2)');}
            }
            \DB::commit();
        } catch (RunException $e) {
            \DB::rollBack();
            return $this->responseFail($e->getMessage());
        } catch (\Exception $e) {
            \DB::rollBack();
            \Log::error($e->getMessage());
            return $this->responseFail('数据库错误');
        }


        return $this->responseSuccess();
    }

    public function setTake()
    {
        $id = (array) $this->request->post('id', []);
        $details = Tables\OrderDetail::whereIn('id', $id)->get();
        if(empty($details)) {
            return $this->responseFail('未找到信息');
        }

        \DB::beginTransaction();
        try {
            foreach($details as $d) {
                $d['make_status'] = Tables\Order::Make_Status_Ok;
                if(!(bool) $d->save()) {throw new RunException('操作失败(2)');}
                $order = $d->order;
                $flag = true;
                foreach($order->details as $od) {
                    if($od['make_status'] != Tables\Order::Make_Status_Ok) {
                        $flag = false;
                        break;
                    }
                }
                if($flag) {
                    $order['make_status'] = Tables\Order::Make_Status_Ok;
                    $order['update_time'] = date('Y-m-d H:i:s');
                    if(!(bool) $order->save()) { throw new RunException('操作失败(3)');}
                }
            }
            \DB::commit();
        } catch (RunException $e) {
            \DB::rollBack();
            return $this->responseFail($e->getMessage());
        } catch (\Exception $e) {
            \DB::rollBack();
            \Log::error($e->getMessage());
            return $this->responseFail('数据库错误');
        }


        return $this->responseSuccess();
    }

}